home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / totdoc.zip / CHAPT18.TXT < prev    next >
Text File  |  1991-02-11  |  15KB  |  334 lines

  1.                                                                        Extending
  2.                                                                          Windows
  3.  
  4.  
  5.          "The success hasn't gone to our price."          Peugeot Motor Company
  6.  
  7.  
  8.  
  9.          Earlier, in chapter 7: Using Windows (page 7-12) a template for
  10.          stretchable windows was presented. This template illustrated how, with
  11.          just a few procedures, you could build customized windows. In this
  12.          chapter you will learn how to use OOP techniques to extend window
  13.          objects, and gain even more flexibility. To illustrate the concepts,
  14.          the StretchWinOBJ object will be extended to provide a generic object
  15.          for scrolling virtual screens.
  16.  
  17.  
  18. Window Object Structure
  19.  
  20.          Before trying to extend the window objects, you need to have an under-
  21.          standing of how the window objects function. The table below describes
  22.          the data declared in each window object.
  23.  
  24.  
  25.                                        WinOBJ
  26.                  Variable                         Description
  27.            vBorder: tCoords     The (X1,Y1,X2,Y2) coordinates of the window
  28.                                 border.
  29.            vOuter: tCoords      The (X1,Y1,X2,Y2) coordinates of the saved
  30.                                 screen area, i.e. window and shadow.
  31.            vUnderneathPtr:      A pointer to the saved image overlaid by the
  32.            pointer              window.
  33.            vSavedSize: longint  The size, in bytes, of the saved area.
  34.            vTitle: string       The window title.
  35.            vBorderAttr          The window display attributes.
  36.            vTitleAttr
  37.            vBodyAttr
  38.            vIconsAttr: byte
  39.            vStyle:byte          The window style.
  40.            vRemove: boolean     A boolean to indicate whether the window will
  41.                                 be removed when the object is Done.
  42.            vCursX,vCursY,       The cursor settings at the time the window was
  43.            vCursTop,            displayed.
  44.            vCursBot: byte
  45.            vOldWin: tByteCoords The window settings at the time the window was
  46.                                 displayed.
  47.            vOldWinConfine:      A boolean to indicate whether window settings
  48.            boolean              were active when the window was displayed.
  49.            vMVisible: boolean   A boolean to indicate whether the mouse was
  50.                                 visible when the window was displayed.
  51.  
  52. 18-2                                                       Extending the Toolkit
  53.  
  54. --------------------------------------------------------------------------------
  55.                                        MoveWinOBJ
  56.                  Variable                         Description
  57.  
  58.  
  59.            vBoundary:tCoords    The (X1,Y1,X2,Y2) coordinates of the area in
  60.                                 which the window can be moved.
  61.            vMoveKey: word       The key to press to invoke a manual (non-mouse)
  62.                                 move.
  63.            vAllowMove:boolean   A boolean to indicate whether the user is
  64.                                 allowed to move the window.
  65.  
  66.                                        ScrollWinOBJ
  67.                  Variable                         Description
  68.            vScrollV:boolean;    A boolean to indicate whether the vertical
  69.                                 scroll bar is visible.
  70.            vScrollH:boolean;    A boolean to indicate whether the horizontal
  71.                                 scroll bar is visible.
  72.  
  73.  
  74.  
  75.                                        StretchWinOBJ
  76.                  Variable                         Description
  77.            vZoomed:boolean      A boolean to indicate whether the window is
  78.                                 currently zoomed.
  79.            vPreZoom:tCoords     The coordinates of the window border prior to
  80.                                 zooming.
  81.            vMinWidth:byte       The minimum width of the window.
  82.            vMinDepth:byte       The minimum depth of the window.
  83.            vStretchKey:word     The key to press to invoke a manual (non-mouse)
  84.                                 stretch.
  85.            vZoomKey:word        The key to press to invoke a manual (non-mouse)
  86.                                 zoom.
  87.            vAllowStretch:       A boolean to indicate whether the user is
  88.            boolean              allowed to stretch the window.
  89.            vSmartStretch:       A boolean to indicate whether the window will
  90.            boolean              be updated during a stretch operation.
  91.  
  92.  
  93.          Note that the type tCoords is defined as follows:
  94.                   tCoords = record
  95.                      X1,Y1,X2,Y2: shortint;
  96.                   end;
  97.  
  98.  
  99.          By now, you should already know most of the window methods in the file
  100.          TOTWIN.PAS. Review the declaration of all four windows objects for a
  101.          complete list of the methods. From an object extension perspective, the
  102.          following methods are important:
  103.          constructor Init;
  104.  
  105.          Initializes the object.
  106.  
  107. Extending Windows                                                           18-3
  108. --------------------------------------------------------------------------------
  109.  
  110.          procedure WinKey(var K:word; var X,Y: byte); VIRTUAL;
  111.  
  112.          WinKey is passed keystroke details as variable parameters. If the key-
  113.          stroke is window specific, e.g. a click on the close icon, a zoom, a
  114.          window stretch etc., the method will process the key and modify the
  115.          relevant window characteristic. The value of K is then updated to indi-
  116.          cate the type of activity, e.g. set to 602 to indicate that the window
  117.          has been re-sized.
  118.          procedure StretchRefresh; VIRTUAL;
  119.  
  120.          While the user is stretching the lower right window, the method Stret-
  121.          chRefresh is continuously called. This is the mechanism for dynamically
  122.          showing the user how the window will appear while it is being
  123.          stretched. The method is only called if the boolean vSmartStretch is
  124.          set to true. There are two important items to consider. Firstly, this
  125.          method will only be called while the window settings are suspended, so
  126.          any screen writing statements in the procedure must use the full-screen
  127.          coordinate system. Secondly, this method must complete its display
  128.          update very quickly. It will be called continuously during a stretch
  129.          operation, and if the procedure takes too long, the user will notice a
  130.          sluggish response.
  131.          procedure Draw; VIRTUAL;
  132.  
  133.          Draw saves the contents of the portion of the screen which will be
  134.          overlaid by the window, and then draws the window and shadow.
  135.          destructor Done; VIRTUAL;
  136.  
  137.          Disposes of the window object memory, and removes the window (if it is
  138.          still visible and the vRemove variable is set to true).
  139.  
  140.          If you extend any window object, you will probably modify most of these
  141.          methods, since they control the window display and input processing
  142.          routines.
  143.  
  144.  
  145.  
  146. Extending StretchWinOBJ
  147.          To illustrate the principles of window extension, a new window object
  148.          for displaying and scrolling virtual screens will be created. You may
  149.          recall that a virtual screen is a screen which has all the properties
  150.          of the physical screen, but is not visible. A virtual screen can be up
  151.          to 255 characters wide and 255 lines deep - quite a screen! The Screen-
  152.          OBJ method PartDisplay can be used to transfer a portion of the virtual
  153.          screen to the visible screen.
  154.  
  155.  
  156.  
  157. 18-4                                                       Extending the Toolkit
  158. --------------------------------------------------------------------------------
  159.  
  160.          The first task in designing the new object is to decide what additional
  161.          data will be needed. Obviously, the object will need to know the
  162.          address of the virtual screen, i.e. a pointer to a ScreenOBJ instance.
  163.          Two additional variables will be required to keep track of which part
  164.          of the virtual screen is on display. In other words, the coordinates of
  165.          the top-left corner of the visible part of the virtual screen. To make
  166.          sure that the user doesn't scroll beyond the bottom-edge or past the
  167.          right-edge of the virtual screen, the object will need to know the
  168.          width and depth of the virtual screen. The preliminary object declara-
  169.          tion might therefore be as follows:
  170.  
  171.          VirtualWinOBJ = object (StretchWinOBJ)
  172.             vScreen: ScreenPtr;
  173.             vTopLine: integer;   {line number of first visible line}
  174.             vFirstChar: integer; {number of left-most visible character}
  175.             vScreenWidth: byte;
  176.             vScreenDepth: byte;
  177.             {Methods...}
  178.             constructor Init;
  179.             procedure   AssignVirtualScreen(var Scr:ScreenOBJ);
  180.             destructor  Done;                                    VIRTUAL;
  181.          end; {VirtualWinOBJ}
  182.  
  183.          So far so good, but the method Draw, inherited from StretchWinOBJ, will
  184.          only draw the window border and then clear the central area of the
  185.          window. VirtualWinOBJ needs to have its own Draw method. This method
  186.          will first call StretchWinOBJ.Draw to display the basic window, and
  187.          will then draw the visible part of the virtual window by calling the
  188.          virtual window's method PartDisplay. The new Draw method will then need
  189.          to update the vertical and horizontal scroll bars to accurately reflect
  190.          which portion of the virtual screen is visible.
  191.  
  192.          The inherited methods DrawHorizbar and DrawVertBar can be used to draw
  193.          the scroll bars. These methods are passed two longint parameters repre-
  194.          senting the current position and maximum possible position. For exam-
  195.          ple, the following statements will correctly update the StretchWinOBJ
  196.          scroll bars:
  197.                   DrawHorizBar(vFirstChar,vScreenWidth);
  198.                   DrawVertBar(vTopLine,vScreenDepth
  199.  
  200.          Since the virtual screen method PartDisplay is mega-fast, we should be
  201.          able to refresh the window display during a stretch. This is achieved
  202.          by adding the method StretchRefresh. All StretchRefresh needs to do is
  203.          restore the visible portion of the virtual screen, and paint any area
  204.          of the window which may be beyond the virtual window boundary.
  205.  
  206.  
  207.  
  208. Extending Windows                                                           18-5
  209. --------------------------------------------------------------------------------
  210.  
  211.          The inherit WinKey method will only process keystrokes which close,
  212.          move or stretch the window. StretchWinOBJ needs to have its own WinKey
  213.          method which calls the inherited one, and then processes any of the
  214.          scrolling keys like End, Home, PgUp, PgDn, etc. as well as any mouse
  215.          clicks on the scroll bars.
  216.  
  217.          Finally, a Go method could be added. This method would continuously get
  218.          user input and call WinKey until the user escaped or clicked on the
  219.          close icon.
  220.          After all these enhancements, the declaration of the full-blown Virtu-
  221.          alWinOBJ would be as follows:
  222.  
  223.          VirtualWinOBJ = object (StretchWinOBJ)
  224.             vScreen: ScreenPtr;
  225.             vTopLine: integer;
  226.             vFirstChar: integer;
  227.             vScreenWidth: byte;
  228.             vScreenDepth: byte;
  229.             {Methods...}
  230.             constructor Init;
  231.             procedure   SetScreenXY(X,Y:byte);
  232.             procedure   AssignVirtualScreen(var Scr:ScreenOBJ);
  233.             procedure   RefreshWindow;
  234.             procedure   ScrollDown;
  235.             procedure   ScrollUp;
  236.             procedure   ScrollLeft;
  237.             procedure   ScrollRight;
  238.             procedure   ScrollTop;
  239.             procedure   ScrollBottom;
  240.             procedure   ScrollHome;
  241.             procedure   ScrollEnd;
  242.             procedure   ScrollJump(Vert:boolean; X,Y:byte);
  243.             procedure   ScrollPgUp;
  244.             procedure   ScrollPgDn;
  245.             procedure   Go;
  246.             procedure   StretchRefresh;                       VIRTUAL;
  247.             procedure   Winkey(var K:word;var X,Y:byte);      VIRTUAL;
  248.             procedure   Draw;                                 VIRTUAL;
  249.             destructor  Done;                                 VIRTUAL;
  250.          end; {VirtualWinOBJ}
  251.  
  252.          The on-disk unit file EXTWIN.PAS contains the complete implementation
  253.          of the VirtualWinOBJ method. You might print out this unit and review
  254.          the detailed statements to gain a thorough understanding of how Virtu-
  255.          alWinOBJ functions. Since all the work has been done for you, you might
  256.          consider creating a descendant of VirtualWinOBJ which includes on-line
  257.          help, printing, etc.
  258.  
  259.  
  260.  
  261.  
  262. 18-6                                                       Extending the Toolkit
  263. --------------------------------------------------------------------------------
  264.  
  265. Using VirtualWinOBJ
  266.  
  267.          Using the new VirtualWinOBJ object is a snap. The demo program EXT-
  268.          DEM4.PAS, listed below, shows how the object could be used to display
  269.          the good 'ole ASCII table. Figure 18.1 shows the display generated by
  270.          the demo program.
  271.  
  272.  
  273.          Program ExtendedDemoFour;
  274.          {EXTDEM4}
  275.          Uses DOS,CRT,
  276.               totINPUT, totFAST, totSTR, extWIN;
  277.  
  278.          var
  279.             ASCIIdata: ScreenOBJ;
  280.             AscWin: VirtualWinOBJ;
  281.          procedure BuildASCIIscreen;
  282.          {creates the virtual screen}
  283.          var
  284.             I,J : integer;
  285.             Str:string;
  286.          begin
  287.             with ASCIIdata do
  288.             begin
  289.                Init;
  290.                Create(65,32,71);
  291.                for I := 0 to 31 do
  292.                begin
  293.                   Str := '';
  294.                   for J := 0 to 7 do
  295.                   begin
  296.                      Str := Str+' '+
  297.                             padright(inttostr(I+32*J),3,'0')+
  298.                             ' '+char(I+32*J)+' ';
  299.                      if J <> 7 then
  300.                         Str := Str + '│';
  301.                   end;
  302.                   WritePlain(1,succ(I),Str);
  303.                end;
  304.                for J := 0 to 7 do
  305.                    Attrib(6+J*8,1,6+J*8,32,78);
  306.             end;
  307.          end; {BuildASCIIscreen}
  308.  
  309.          begin
  310.             Screen.Clear(white,'░'); {paint the screen}
  311.             Key.SetFast;
  312.             BuildASCIIscreen;
  313.             with AscWin do
  314.  
  315.  
  316.  
  317. Extending Windows                                                           18-7
  318. --------------------------------------------------------------------------------
  319.  
  320.             begin
  321.                Init;
  322.                SetTitle(' ASCII Table ');
  323.                AssignVirtualScreen(ASCIIdata);
  324.                Go;
  325.                ASCIIdata.Done;
  326.                Done;
  327.             end;
  328.          end.
  329.  
  330.  
  331. Figure 18.1                                                             [SCREEN]
  332. Using
  333. VirtualWinOBJ
  334.